﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WSAP;

namespace CouplingBeamStiff
{
    class Block:Rectangular
    {
        /// <summary>
        /// 编号
        /// </summary>
        private int Index { get; set; }

        /// <summary>
        /// 是否为梁单元块
        /// </summary>
        private bool IsBeam { get; set; }

        /// <summary>
        /// 单元集合
        /// </summary>
        public List<Element> Elements { get; set; }

        /// <summary>
        /// 获得块体的节点集合
        /// </summary>
        /// <returns></returns>
        public List<Node> GetNodelist()
        {
            var nodelist = new List<Node>();
            this.Elements.ForEach(element => nodelist.AddRange(element.GetNodelist()));
            return nodelist;
        }

        /// <summary>
        /// 添加块体
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool AddBlock(WModelNet model)
        {
            foreach(var element in this.Elements)
            {
                if (!element.AddElement(model))
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 划分网格单元
        /// </summary>
        public void Mesh()
        {
            //划分尺寸
            var scale = GlobalVar.MeshDimension;
            this.Elements = new List<Element>();
            int NumX, NumY;
            //划分梁单元
            if (IsBeam)
            {
                NumX = (int)(this.Width / scale);
                for(int i = 0; i < NumX; i++)
                {
                    var ww = i == NumX - 1 ? this.Width - i * scale : scale;
                    var hh = this.Height;
                    var ix = this.NodeI.X + scale * i;
                    var iy = this.NodeI.Y;
                    Elements.Add(new Element(ww, hh, ix, iy, this.Material, true));
                }
            }
            //横向纵向划分数目
            NumX = (int)(this.Width / scale);
            NumY = (int)(this.Height / scale);
            //划分壳单元
            if (NumX != 0 && NumY != 0)
            {
                for (int j = 0; j < NumY; j++)
                {
                    for (int i = 0; i < NumX; i++)
                    {
                        var ww = i == NumX - 1 ? Width - i * scale : scale;
                        var hh = j == NumY - 1 ? Height - j * scale : scale;
                        var ix = this.NodeI.X + scale * i;
                        var iy = this.NodeI.Y + scale * j;
                        Elements.Add(new Element(ww, hh, ix, iy, this.Material,false));
                    }
                }
            }
            else if (NumX == 0 && NumY != 0)
            {
                for (int j = 0; j < NumY; j++)
                {
                    var ww = Width;
                    var hh = j == NumY - 1 ? Height - j * scale : scale;
                    var ix = this.NodeI.X;
                    var iy = this.NodeI.Y + scale * j;
                    Elements.Add(new Element(ww, hh, ix, iy, this.Material,false));
                }
            }
            else if (NumX != 0 && NumY == 0)
            {
                for (int i = 0; i < NumX; i++)
                {
                    var ww = i == NumX - 1 ? Width - i * scale : scale;
                    var hh = Height;
                    var ix = this.NodeI.X + scale * i;
                    var iy = this.NodeI.Y;
                    Elements.Add(new Element(ww, hh, ix, iy, this.Material,false));
                }
            }
            else
            {
                Elements.Add(new Element(Width, Height, this.NodeI.X, this.NodeI.Y, this.Material,false));
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="nodeI"></param>
        /// <param name="material"></param>
        public Block(int index, double width, double height, double x, double y, Material material, bool isbeam)
            :base(width, height, x, y, material)
        {
            this.IsBeam = isbeam;
            this.Index = index;
        }
        
    }
}
